package com.znsd.r_e_m.dao.imp;

import com.alibaba.fastjson.JSONObject;
import com.znsd.r_e_m.bean.Student;
import com.znsd.r_e_m.dao.EsDataGet;
import com.znsd.r_e_m.dao.StudentMapper;
import com.znsd.r_e_m.utils.RedisUtils;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Repository
public class EsDataGetImp implements EsDataGet {

    @Autowired
    ElasticsearchTemplate template;
    @Autowired
    private RedisUtils redisUtils;

    public List<Student> queryAll(){
        NativeSearchQuery search = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.matchAllQuery())
                .build();
        List<Student> students = template.queryForList(search, Student.class);
        System.out.println(students.size());
        return students;
    }

    @Override
    public List<Student> queryStudentByName(String name) {
        NativeSearchQuery search = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.termQuery(name,"stuname"))
                .build();
        return template.queryForList(search,Student.class);
    }

    @Override
    public List<Student> queryAll(Integer page,Integer size) {
        System.out.println(page+"-----"+size);
        NativeSearchQuery search = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.matchAllQuery())
                .withPageable(PageRequest.of(page,size,new Sort(Sort.Direction.ASC,"stuId")))
                .build();
        List<Student> students = template.queryForList(search, Student.class);
        String key = page+"-"+size;

        String s = JSONObject.toJSONString(students);
        System.out.println(students);
        redisUtils.set(key,s,30L, TimeUnit.SECONDS);

        return students;
    }

    @Override
    public List<Student> globalSearch(String keyword,Integer page,Integer size) {
        NativeSearchQuery search = new NativeSearchQueryBuilder()
                .withPageable(PageRequest.of(page,size,new Sort(Sort.Direction.ASC,"stuId")))
                .withQuery(QueryBuilders.queryStringQuery(keyword))
                .build();
        List<Student> students = template.queryForList(search, Student.class);
        return students;
    }

    @Override
    public int globalSearchTotal(String keyword) {
        NativeSearchQuery search = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.queryStringQuery(keyword))
                .build();
        List<Student> students = template.queryForList(search, Student.class);
        return students.size();
    }
}